/*
 * Copyright (C) 2014 Lucid Wolf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.lucidhex.viewer;

import com.lucidhex.LucidWorld;
import com.lucidhex.hex.LucidHex;
import com.lucidhex.hex.LucidHexType;
import com.lucidhex.hex.PlaneCoord;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Wolf
 */
public class ViewRenderer {
    final static Color COLOURBACK =  Color.WHITE;
    final static Color COLOURTEXT =  Color.BLACK;
    final static Color COLOURSELECTED =  Color.RED;	 
    final static Color COLOURGRID =  Color.BLACK;	 
    final static Color COLOURONE = new Color(255,255,255,200);
    final static double CELLVIEWSCALELIMIT = 16.0;
    final static double innerR = 0.866025;
    
       //
    private ViewOptions vo;
    private Graphics2D g2D;
    private double scaleFactor = 1.0;
    private HashMap<LucidIcon, BufferedImage> rotatedIcons = new HashMap(); 
    private final LucidWorld world;
    private double theta = 0.0;
    private final ViewPortal vp;

    public ViewRenderer(ViewPortal vp, ViewOptions vo, LucidWorld world) {
        this.vp = vp;
        this.vo = vo;
        this.world = world;
    }
    public void updatePencil(Graphics g) {
        g2D = (Graphics2D) g.create();
        g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        double oScale = scaleFactor;
        scaleFactor = vo.getDrawingScale();
        double oTheta = theta;
        theta = vo.getTheta();
        if(oScale != scaleFactor || oTheta != theta){
            rotatedIcons = new HashMap();
            for(LucidHexType lht : world.getBoard().getAvailbleHexTypes()){
                LucidIcon li = lht.getForegroundIcon();
                if(li != null && scaleFactor > CELLVIEWSCALELIMIT){
                    rotatedIcons.put(li, li.getRotatedImage(scaleFactor*innerR, theta));
                    if(true){
                        ImageIcon ii = new ImageIcon(rotatedIcons.get(li));
                        //JOptionPane.showMessageDialog(vp, "Icon Check", "IconCheck", JOptionPane.PLAIN_MESSAGE, ii);
                    }
                }
            }
        }
    }
    
    /********************************************************************
    Name: drawHex()
    Parameters: (i,j) : the x,y coordinates of the initial point of the hexagon
                g2: the Graphics2D object to draw on.
    Returns: void
    Calls: hex() 
    Purpose: This function draws a hexagon based on the initial point (x,y).
    The hexagon is drawn in the color specified in COLOURELL.
    *********************************************************************/
    public void drawHex(LucidHex eHex) {
        PlaneCoord mid = new PlaneCoord(eHex.getPosition());
        int p[] = vo.transform(mid);
        // radius of all hex are 1 to make it simple
        // inner radius is then cos(30) * 1 which is what is used for image inlay
        if(vo.isInBounds(mid)){
            int cx[] = new int[6];
            int cy[] = new int[6];
            int i = 0;
            for(double rad = 0.523599; rad < 6.28; rad=rad+1.047189){
                PlaneCoord aS = new PlaneCoord(mid.X()+ Math.cos(rad), mid.Y()+ Math.sin(rad));
                int p1[] = vo.transform(aS);
                cx[i] = p1[0];
                cy[i] = p1[1];
                 i++;
            }
            Polygon poly = new Polygon(cx,cy,6);
            g2D.setColor(eHex.getBackGroundColor());
            g2D.fillPolygon(poly);
            if(scaleFactor > CELLVIEWSCALELIMIT){
               g2D.setColor(COLOURGRID);
               g2D.drawPolygon(poly);
               LucidIcon icon = eHex.getHexDetail();
               if(icon != null){
                   BufferedImage bi = rotatedIcons.get(icon);
                   int picx = p[0]-(int)(bi.getWidth()/2.0);
                   int picy = p[1]-(int)(bi.getHeight()/2.0);
                   g2D.drawImage(bi, null, picx, picy);
               }
            }
            //g2D.setColor(COLOURGRID);
            //g2D.drawPolygon(poly);
        }
    }
    public void drawHexLabels(LucidHex eHex) {
        PlaneCoord mid = new PlaneCoord(eHex.getPosition());
        int p[] = vo.transform(mid);
        g2D.setColor(COLOURTEXT);
        g2D.drawString(eHex.toString(), p[0], p[1]);        
    }

    public void drawSelectedHex(LucidHex eHex) {
        if(eHex == null){return;}
        if(eHex.getPosition() == null){return;}
        PlaneCoord mid = new PlaneCoord(eHex.getPosition());
        if(vo.isInBounds(mid)){
            int cx[] = new int[6];
            int cy[] = new int[6];
            int i = 0;
            for(double rad = 0.523599; rad < 6.28; rad=rad+1.047189){
                PlaneCoord aS = new PlaneCoord(mid.X()+ Math.cos(rad), mid.Y()+ Math.sin(rad));
                int p[] = vo.transform(aS);
                cx[i] = p[0];
                cy[i] = p[1];
                i++;
            }
            Polygon poly = new Polygon(cx,cy,6);
            g2D.setColor(COLOURSELECTED);
            g2D.drawPolygon(poly);
        }
        
    }

}
